Published on

Exploring the ps Linux command

Authors
  • avatar
    Name
    Carter Speerschneider
    Twitter

Command Overview

The ps command is a UNIX command that displays information on all the processes at the time the command executed in the form of a formattable table with records representing individual processes. Importantly, ps only returns a snapshot and not a real-time feed of process information which has it's drawbacks.

Nonetheless, ps is a very useful command for just getting a quick and dirty summary of all the processes on your systems and if customized correctly, it can be a very insightful tool.

Interesting Options

There are several ways to write the flags for ps which might make it a bit confusing to beginners. I stick with the unix style (single-dashed options) of options to avoid confusion, but as long as you're consistent, it doesn't matter which you choose.

The first command you are likely to see shows all the running processes:

ps -ef

-e shows every process -f shows all standard columns in the output table

Although this is a good start, one way we can enhance this is by replacing a standard format , -f, with the a user-orientated output, -u.

ps -eu

You'll now see some columns that tell you information about the system resource utilization: %CPU, %MEM. You'll also notice that STAT column which tells you the state of the process. This is a very useful concept to know as this gives you a glimpse into what behavior your processes are exhibiting at any given time.

Building your own layout

Sometimes we don't want to view the layout that is predefined in these flags; we prefer a bit more control over the output.

To do this, we can use the -o option that takes a comma-delimited list of format specifiers for every column to include in the process table output.

ps -e -o user,state,pid,comm

This is very basic instance of what you can do with this option. The acceptable values are endless but for basic output, these may do.

Here is an example output on my machine:

USER     S     PID COMMAND
root     S       1 systemd
root     S       2 init-systemd(De
root     S       6 init
root     S      36 systemd-journal
root     S      75 systemd-udevd
root     S     142 cron
message+ S     143 dbus-daemon
root     S     147 systemd-logind
root     S     180 agetty
root     S     181 agetty
root     S     187 SessionLeader
root     S     188 Relay(189)
carterd+ S     189 bash
root     S     190 login
carterd+ S     200 systemd
carterd+ S     202 (sd-pam)
carterd+ S     230 bash
carterd+ R     448 ps

If you are particularly fond of a certain output, feel free to make an alias in your .bash_aliases file or similar to help you abstract process summarization with a single command!

Summary

The ps command is a simple but powerful tool that can be used to see various process information such as process IDs (PIDs), resource utilization, and even thread count (thcount to -o). While there are very more specialized applications for system summarization on Linux, knowing where it all begins helps you understand your system on a more fundamental level as well as more advanced applications.